package com.gupao.coupon.portal.couponportal.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.rpc.RpcContext;
import org.gupao.normal.example.ICouponService;
import org.gupao.normal.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @Author: Mic
 * @公众号： 跟着Mic学架构
 * @官网： https://ke.gupaoedu.cn
 * @Org: 咕泡教育
 **/
@RestController
public class UserController {

//    @DubboReference(check = false,timeout = 3000,mock = "com.gupao.coupon.portal.couponportal.mock.MockUserService")
    @DubboReference(async = true)
    IUserService userService;
    @DubboReference
    ICouponService couponService;

    @GetMapping("/coupon")
    public String validCoupons() throws ExecutionException, InterruptedException {
        userService.selectValidTemplates(); //不会阻塞
        System.out.println(Thread.currentThread().getName()+": 不等待接口返回，直接先执行");
        CompletableFuture<String> future=RpcContext.getServerContext().getCompletableFuture();
        //TODO 1 ,处理其他业务逻辑
        //v, 返回的结果
        //e, 返回的异常
        //Callable+Future
        //TODO 2,
        future.whenComplete((v,e)->{
            if(e==null){
                System.out.println("返回正常的处理结果："+v);
            }else{
                e.printStackTrace();
            }
        });
        System.out.println(Thread.currentThread().getName()+": 不等待一部返回，继续执行后续代码");
        return "处理成功";
    }
}
